package ro.gii.auctionHouse.service.impl;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import ro.gii.auctionHouse.dao.UserDAO;
import ro.gii.auctionHouse.model.User;
import ro.gii.auctionHouse.service.UserService;
import ro.gii.auctionHouse.util.Util;

@Service(value = "userService")
@Transactional(readOnly = true)
public class UserServiceImpl implements UserService{
	
	@Autowired
	private UserDAO userDAO;
	
/*	@Override
	@PreAuthorize("hasRole('ROLE_USER')")O
	public String getUserRoles(List<UserRole> userRoles) {
		StringBuilder roles = new StringBuilder();
		Iterator<UserRole> it = userRoles.iterator();
		while (it.hasNext()) {
			UserRole userRole = it.next();
			roles.append(userRole.getRole().getCode());
			if (it.hasNext()) {
				roles.append(", ");
			}
		}
		return roles.toString();
	}*/



	@Override
	public boolean loginUser(String login, String password) {
		User user = this.getUserByLogin(login);
		try {
			password = Util.encryptMD5Password(password);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return user != null && user.getPassword().equals(password);
	}
	
	@Override
	public User getUserByLogin(String login) {
		return this.userDAO.findByLogin(login);
	}

	@Override
	public User getUserById(Integer id) {
		return this.userDAO.findById(id);
	}

}
